package com.yygo.service.impl;

import com.yygo.dao.VehicleBrandDao;
import com.yygo.model.VehicleBrand;
import com.yygo.model.VehicleSeries;
import com.yygo.service.VehicleBrandManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * Created by tiansha on 2015/6/23.
 */
@Service("vehicleBrandManager")
public class VehicleBrandManagerImpl extends GenericManagerImpl<VehicleBrand, Long> implements VehicleBrandManager {

    private VehicleBrandDao vehicleBrandDao;

    @Autowired
    public VehicleBrandManagerImpl(VehicleBrandDao vehicleBrandDao) {
        super(vehicleBrandDao);
        this.vehicleBrandDao = vehicleBrandDao;
    }

    @Override
    public void clearAll() {
        this.vehicleBrandDao.clearAll();
    }

    @Override
    public List<VehicleSeries> getMySeries(String brandName, boolean isSecond) {
        Set<VehicleSeries> vehicleSeries = vehicleBrandDao.getMySeries(brandName, isSecond);
        List<VehicleSeries> vehicleSeriesList = new ArrayList<VehicleSeries>();
        vehicleSeriesList.addAll(vehicleSeries);
        vehicleSeriesList.sort(new Comparator<VehicleSeries>() {
            @Override
            public int compare(VehicleSeries o1, VehicleSeries o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        return vehicleSeriesList;
    }

    @Override
    public List<VehicleBrand> getAllBrands(boolean isSecond) {
        Set<VehicleBrand> vehicleBrands = vehicleBrandDao.getAllBrands(isSecond);
        List<VehicleBrand> vehicleBrandList = new ArrayList<VehicleBrand>();
        vehicleBrandList.addAll(vehicleBrands);
        vehicleBrandList.sort(new Comparator<VehicleBrand>() {
            @Override
            public int compare(VehicleBrand o1, VehicleBrand o2) {
                return o1.getBrand().compareTo(o2.getBrand());
            }
        });
        return vehicleBrandList;
    }
}
